﻿#include "cscreenprotectordlg.h"
#include "ui_cscreenprotectordlg.h"
#include <QDate>
#include <QTime>
#include <QPainter>
#include <QDebug>



void CScreenProtectorDlg::Initialize()
{
    // delete resource when close window
    setAttribute(Qt::WA_DeleteOnClose);
    // FrameLess & Z-Pos Max
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    // launch timer for draw text
    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(OnTimerOut()));
    m_timer->start(130);
}

CScreenProtectorDlg::CScreenProtectorDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CScreenProtectorDlg)
{
    ui->setupUi(this);
    Initialize();
}

CScreenProtectorDlg::~CScreenProtectorDlg()
{
    delete ui;
}

const ss::RunConfig& CScreenProtectorDlg::GetRuntimeSettings()
{
    return ss::CRuntimeSetting::GetConfig();
}


void CScreenProtectorDlg::Draw()
{
    // hour minute second's backgroud rounded rectange width & height
    // which can fit for show or NOT show second field
    int nBorderSpace = GetRuntimeSettings().bShowSecond? 50 : 150;
    int cnt = GetRuntimeSettings().bShowSecond? 1 : 0;
    int w = (geometry().width() - nBorderSpace * (3 + cnt)) / (2 + cnt);
    int h = w + 30;

    QPainter painter(this);
    QFont fontForTime;
    fontForTime.setFamily(GetRuntimeSettings().font);
    fontForTime.setBold(true);
    // do NOT use config file
    fontForTime.setPixelSize(min(w, h) - 50);



    // 时间的每个数字分别绘制，兼容非等宽字体的情况，非等宽字体在秒变化时两个十位数
    // 之间的间隔变化频繁效果不好，分开绘制则可解决
    int hour = QTime::currentTime().hour();
    QString hourHigh = QString::number(hour / 10);
    QString hourLow = QString::number(hour - (hour / 10 * 10));
    int minute = QTime::currentTime().minute();
    QString minuteHigh = QString::number(minute / 10);
    QString minuteLow = QString::number(minute - (minute / 10 * 10));
    int second = QTime::currentTime().second();
    QString secondHigh = QString::number(second / 10);
    QString secondLow = QString::number(second - (second / 10 * 10));

    QString date = QDateTime::currentDateTime().toString("yyyy年MM月dd日 dddd");
    painter.setFont(fontForTime);

    // draw black backgroud
    painter.setBrush(Qt::black);
    painter.drawRect(0, 0, geometry().width(), geometry().height());

    // usedraw rounded rectangle
    QBrush brushRect(QColor(0x12, 0x12, 0x12));
    painter.setBrush(brushRect);
    int nTopBottomSpace = 20, nLeftRightSpace = 20;
    // hour
    QRect rcHour(nBorderSpace, (geometry().height() - h) / 2, w, h);
    QRect rcHourHigh(rcHour.left() + nLeftRightSpace,
                     rcHour.top() + nTopBottomSpace,
                     rcHour.width() / 2 - nLeftRightSpace - nLeftRightSpace / 2,
                     rcHour.height() - nTopBottomSpace * 2);
    QRect rcHourLow(rcHour.left() + w / 2 + nLeftRightSpace / 2,
                    rcHour.top() + nTopBottomSpace,
                    rcHour.width() / 2 - nLeftRightSpace - nLeftRightSpace / 2,
                    rcHour.height() - nTopBottomSpace * 2);
    painter.drawRoundedRect(rcHour, 50, 50);
    // minute
    QRect rcMinute(nBorderSpace * 2 + w, (geometry().height() - h) / 2, w, h);
    QRect rcMinuteHigh(rcMinute.left() + nLeftRightSpace,
                       rcMinute.top() + nTopBottomSpace,
                       rcMinute.width() / 2 - nLeftRightSpace - nLeftRightSpace / 2,
                       rcMinute.height() - nTopBottomSpace * 2);
    QRect rcMinuteLow(rcMinute.left() + w / 2 + nLeftRightSpace / 2,
                      rcMinute.top() + nTopBottomSpace,
                      rcMinute.width() / 2 - nLeftRightSpace - nLeftRightSpace / 2,
                      rcMinute.height() - nTopBottomSpace * 2);
    painter.drawRoundedRect(rcMinute, 50, 50);
    // second
    QRect rcSecond(nBorderSpace * 3 + w * 2, (geometry().height() - h) / 2, w, h);
    QRect rcSecondHigh(rcSecond.left() + 20,
                       rcSecond.top() + nTopBottomSpace,
                       rcSecond.width() / 2 - nLeftRightSpace - nLeftRightSpace / 2,
                       rcSecond.height() - nTopBottomSpace * 2);
    QRect rcSecondLow(rcSecond.left() + w / 2 + nLeftRightSpace / 2,
                      rcSecond.top() + nTopBottomSpace,
                      rcSecond.width() / 2 - nLeftRightSpace - nLeftRightSpace / 2,
                      rcSecond.height() - nTopBottomSpace - nLeftRightSpace / 2);
    if (GetRuntimeSettings().bShowSecond)
    {
        painter.drawRoundedRect(rcSecond, 50, 50);
    }

    // draw text with white pen
    painter.setPen(QColor(0xae, 0xae, 0xae));
    painter.drawText(rcHourHigh, Qt::AlignCenter | Qt::AlignHCenter, hourHigh);
    painter.drawText(rcHourLow, Qt::AlignCenter | Qt::AlignHCenter, hourLow);
    painter.drawText(rcMinuteHigh, Qt::AlignCenter | Qt::AlignHCenter, minuteHigh);
    painter.drawText(rcMinuteLow, Qt::AlignCenter | Qt::AlignHCenter, minuteLow);
    if (GetRuntimeSettings().bShowSecond)
    {
        painter.drawText(rcSecondHigh, Qt::AlignCenter | Qt::AlignHCenter, secondHigh);
        painter.drawText(rcSecondLow, Qt::AlignCenter | Qt::AlignHCenter, secondLow);
    }

    if (GetRuntimeSettings().bShowDate)
    {
        QFont fontForDate;
        fontForDate.setFamily(GetRuntimeSettings().font);
        fontForDate.setBold(true);
        fontForDate.setPixelSize(GetRuntimeSettings().nFontSizeForDate);

        QFontMetrics fmForDate(fontForDate);
        int widthForDate = fmForDate.horizontalAdvance(date);
        int heightForDate = fmForDate.height();

        painter.setFont(fontForDate);
        painter.drawText((geometry().width() - widthForDate) / 2, max(0, rcHour.top() - 30 - heightForDate),
                        widthForDate, heightForDate, Qt::AlignCenter | Qt::AlignHCenter, date);
    }

    const QString& sentence = GetRuntimeSettings().sentence;
    if (sentence.length() > 0)
    {
        QFont fontForSentence;
        fontForSentence.setFamily(GetRuntimeSettings().font);
        fontForSentence.setBold(true);
        fontForSentence.setPixelSize(GetRuntimeSettings().nFontSizeForSentence);

        QFontMetrics fmForSentence(fontForSentence);
        int widthForSentence = fmForSentence.horizontalAdvance(sentence);
        int heightForSentence = fmForSentence.height();

        auto h = ((geometry().height() + rcHour.bottom()) / 2) - (heightForSentence / 2);
        painter.setFont(fontForSentence);
        painter.drawText((geometry().width() - widthForSentence) / 2, min(geometry().height() - heightForSentence, h),
                        widthForSentence, heightForSentence, Qt::AlignCenter | Qt::AlignHCenter, sentence);
    }

#if 1
    // black line cross time
    painter.fillRect(0, geometry().height() / 2 - 5, geometry().width(), 5 * 2, Qt::black);
#endif
}

void CScreenProtectorDlg::OnTimerOut()
{
    update();
}

void CScreenProtectorDlg::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    Draw();
}

void CScreenProtectorDlg::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Escape)
    {
        e->ignore();
    }
}
